package controllers;

import helpers.CronHelper;
import helpers.DateHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.AutoSiteData;

import models.AutoSite;

public class CronAutoSiteLastestNews extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		response.setContentType("text/plain");
		ArrayList<AutoSite> sites = AutoSiteData.getAlls();
		AutoSite site = new AutoSite();
		for (int i = 0; i < sites.size(); i++) {
			site = sites.get(i);
			if (!DateHelper.checkGreaterNow(site.getLastestScheduleSite(), site.getScheduleSite())) {
				CronHelper.cronLastestNews(site.getId());
				site.setLastestScheduleSite(new Date());
				AutoSiteData.insert(site);
				response.getWriter().println("Cron success autoSite id: " + site.getId());
			} else {
				response.getWriter().println("Cron unsuccess autoSite id: " + site.getId());
			}
		}
	}
}
